package com.xdap.common.util;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;

@Slf4j
public class ApaasBase64Util {

    public static final String KEYWORD = "BAIDU_Lss+ylx!23";

    /**
     * des 加密
     *
     * @param plainText
     * @param desKeyParameter 加密秘钥
     * @return 二进制字节数组
     * @throws Exception
     */
    public static String encrypt(String plainText, String desKeyParameter) {
        try {
            SecureRandom sr = new SecureRandom();
            byte[] rawKeyData = desKeyParameter.getBytes();
            DESKeySpec dks = new DESKeySpec(rawKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key, sr);
            byte[] data = plainText.getBytes(StandardCharsets.UTF_8);
            byte[] encryptedData = cipher.doFinal(data);
            String resultCode = Base64.getEncoder().encodeToString(encryptedData);
            if (resultCode.contains("+")) {
                resultCode = resultCode.replace("+", "%2B");
            }
            return resultCode;
        } catch (Exception e) {
            log.error("加密出现问题", e);
        }
        return null;
    }


    /**
     * des 解密
     *
     * @param encryptText
     * @param desKeyParameter 解密秘钥
     * @return 二进制字节数组
     * @throws Exception
     */
    public static String decrypt(String encryptText, String desKeyParameter) {
        try {
            if (encryptText.contains("%2B")) {
                encryptText = encryptText.replaceAll("%2B", "+");
            }
            if (encryptText.contains("%252B")) {
                encryptText = encryptText.replaceAll("%252B", "+");
            }
            log.info("更换+后的参数为，{}", encryptText);
            byte[] encryptedData = Base64.getDecoder().decode(encryptText.getBytes());

            SecureRandom sr = new SecureRandom();
            byte[] rawKeyData = desKeyParameter.getBytes();
            DESKeySpec dks = new DESKeySpec(rawKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key, sr);
            byte[] decryptedData = cipher.doFinal(encryptedData);
            return new String(decryptedData);
        } catch (Exception e) {
            log.error("解密出现问题", e);
        }
        return null;
    }

    // public static void main(String args[]) {
    //     String v_zhenghehuizi02 = ApaasBase64Util.encrypt("v_lishushu", ApaasBase64Util.KEYWORD);
    //
    //
    //     System.out.println(v_zhenghehuizi02);
    //     //    System.out.println(ApaasBase64Util.decrypt("Eg1%2Bv8GcwGFLgaqMyDiBRg", ApaasBase64Util.KEYWORD));
    //     //     String name = "lengfangping";//dl9saXNodXNodTAx
    //     String encode = ApaasBase64Util.decrypt(v_zhenghehuizi02, ApaasBase64Util.KEYWORD);
    //     System.out.println(encode);
    //
    // }

}
